﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Text;

namespace YXShop.Web.admin.systeminfo
{
    public partial class mouldinfo_edit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.Page.IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("010006002","对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("010006004","对不起，您没有权限进行编辑");
                InitWebControl();
                GetModel();
            }
        }
        private void GetModel()
        {
            int id = ChangeHope.WebPage.PageRequest.GetQueryInt("id");
            if (id > 0)
            {
                BLL.SystemInfo.MouldInfo bll = new YXShop.BLL.SystemInfo.MouldInfo();
                Model.SystemInfo.MouldInfo model = bll.GetModel(id);
                if(model!=null)
                {
                    this.txtId.Value = model.Id.ToString();
                    this.txtDescription.Text = model.Description;
                    this.txtName.Text = model.Name;
                    this.ckbStat.SelectedValue = model.Stat.ToString();
                    this.ddlMouldType.SelectedValue = model.MouldType.ToString();
                    this.txtWebPagePath.Text = model.WebPagePath;
                }
                model = null;
                bll = null;
            }
        }
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.SetDropDownList(this.ddlMouldType, "Id", "name", "yxs_mouldtype");
            ChangeHope.WebPage.WebControl.Validate(this.txtDescription,"该模型的详细说明","isnull","必填","该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "该模型的名称", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtWebPagePath, "该模型存储路径", "isnull", "必填", "该项为必填项");
            this.Form.Attributes.Add("onsubmit","return CheckForm()");
            this.txtWebPagePath.Attributes.Add("onclick", "GetFile(this)");
            this.txtWebPagePath.Attributes.Add("readonly","readonly");
            GetFileList();
        }
        private void GetFileList()
        {
            /*
            修改人：ym
            修改时间：2009-8-26
            修改内容：添加
             */
            YXShop.Common.SysParameter sp = new YXShop.Common.SysParameter();
            ChangeHope.Common.FileHelper file = new ChangeHope.Common.FileHelper();

            StringBuilder filelist = new StringBuilder();
            string isStoreLabel = ChangeHope.WebPage.PageRequest.GetString("isStoreLabel");
            if (isStoreLabel == "0")
            {
                file.rootUrl = Server.MapPath("~/" + sp.WebSiteTemplatePath);
            }
            else
            {
                file.rootUrl = Server.MapPath("~/templates/store_template");
            }

            file.listFiles(file.rootUrl, 0);

            filelist.AppendLine("<script type=\"text/javascript\">");
            filelist.AppendLine("d = new dTree('d');");
            filelist.AppendLine("d.add(0,-1,'请选择模版');");
            filelist.AppendLine(file.fileTree.ToString());
            filelist.AppendLine("document.write(d);");
            filelist.AppendLine("$(\"fileLists\").style.visibility=\"hidden\";");
            filelist.AppendLine("</script>");
            this.ltlFileList.Text = filelist.ToString();
            file = null;
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            BLL.SystemInfo.MouldInfo bll = new YXShop.BLL.SystemInfo.MouldInfo();
            Model.SystemInfo.MouldInfo model = new YXShop.Model.SystemInfo.MouldInfo();
            Model.SystemInfo.MouldInfo modelname = bll.GetModelName(this.txtName.Text); 
            try
            {
                model.Id = ChangeHope.Common.StringHelper.StringToInt(this.txtId.Value);
                model.MouldType = ChangeHope.Common.StringHelper.StringToInt(this.ddlMouldType.SelectedValue);
                model.Name = this.txtName.Text;
                model.Stat = ChangeHope.Common.StringHelper.StringToInt(this.ckbStat.SelectedValue);
                model.Description = this.txtDescription.Text;
                model.WebPagePath = this.txtWebPagePath.Text;
                if (model.Id > 0)
                {
                    if (modelname != null)
                    {
                        if (modelname.Id != model.Id)
                        {
                            this.ltlMsg.Text = "保存失败,模型名称不能重复,请重新输入！";
                            this.pnlMsg.CssClass = "actionErr";
                            return;
                        }
                    }
                    bll.Update(model);
                }
                else
                {
                    if (modelname != null)
                    {
                        this.ltlMsg.Text = "保存失败,模型名称不能重复,请重新输入！";
                        this.pnlMsg.CssClass = "actionErr";
                        return;
                    }
                    this.txtId.Value = bll.Add(model).ToString();
                }
                model = null;
                bll = null;
                this.ltlMsg.Text = "保存成功！";
                this.pnlMsg.CssClass = "actionOk";
            }
            catch {
                this.ltlMsg.Text = "保存失败！";
                this.pnlMsg.CssClass = "actionErr";
                
            }
            finally
            {
                this.pnlMsg.Visible = true;
                model = null;
                bll = null;
            }

        }
    }
}
